﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KandaAlpha.Domain.Model.Entities;
using KandaAlpha.Domain.Repository;
using System.Data;
using System.Linq.Expressions;
using System.Threading;
using System.Web;
using Db4objects.Db4o;

namespace KandaAlpha.Infrastructure.Repository.db4o
{

    public class RepositoryContext : IRepositoryContext
    {

        private IObjectContainerManager _objectContainerManager;

        public RepositoryContext(IObjectContainerManager objectContainerManager)
        {
            _objectContainerManager = objectContainerManager;
        }

        public int SaveChanges()
        {
            // TODO: return number of objects affected if possible, not sure if db4o supports this
            try
            {
                _objectContainerManager.ObjectContainer.Commit();
                return 0;
            } 
            catch(Exception ex)
            {
                throw new Exception("db4o problem encountered saving changes", ex);
            }

        }

        public void Terminate()
        {
            _objectContainerManager.ObjectContainer.Close();
        }

    }
}
